package com.main.toyexchange;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bjtu.toyexchange.R;
import com.item.toyexchange.ChatActivity;

public class XiaoxiActivity extends Activity {

	private ListView listview;
	private LayoutInflater layout;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_xiaoxi);
		
		findViewById();
	}
	
	protected void findViewById() {
		listview = (ListView)this.findViewById(R.id.xiaoxiList);
		listview.setAdapter(new CatergorAdapter());
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterview, View view, int parent,
					long id) {
				Intent intent = new Intent();
				String sender = mSenderValues[parent];
				intent.putExtra("sender", sender);
				intent.setClass(XiaoxiActivity.this, ChatActivity.class);
				XiaoxiActivity.this.startActivity(intent);
				
			}
		});
	}
	private class CatergorAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mImageIds.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = new ViewHolder();
			layout = LayoutInflater.from(XiaoxiActivity.this);
			//组装数据
			if(convertView==null){
				convertView = layout.inflate(R.layout.xiaoxi_item, null);
				holder.pic = (ImageView)convertView.findViewById(R.id.mes_pic);
				holder.sender = (TextView)convertView.findViewById(R.id.mes_sender);
				holder.content = (TextView)convertView.findViewById(R.id.mes_info);
				//使用tag存储数据
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder)convertView.getTag();
			}
			holder.pic.setImageResource(mImageIds[position]);
			holder.sender.setText(mSenderValues[position]);
			holder.content.setText(mContentValues[position]);
			
			return convertView;
		}
	}
	// 适配显示的图片数组
	private Integer[] mImageIds = {R.drawable.pic,R.drawable.pic,R.drawable.pic,R.drawable.pic,
			R.drawable.pic,R.drawable.pic,R.drawable.pic,R.drawable.pic
			 };
	//给照片添加文字显示(Title)
	private String[] mSenderValues = { "用户名1", "用户名2", "用户名3", "用户名4", "用户名5",
			"用户名6", "用户名7", "用户名8" };
	
	private String[] mContentValues={"聊天 = =", "聊天 = =", "聊天 = =", "聊天 = =", "聊天 = =", 
			"聊天 = =", "聊天 = =", "聊天 = ="};
			

	 public static class ViewHolder {
			ImageView pic;
			TextView sender;
			TextView content;
	}
}
